#include "TurnScoreSelectDlg.h"
#include "ui_TurnScoreSelectDlg.h"
#include <QCloseEvent>
#include <QDebug>

TurnScoreSelectDlg::TurnScoreSelectDlg(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::TurnScoreSelectDlg),
    mSelectedScore(-1),
    mCurSelected(NULL)
{
    ui->setupUi(this);
    setWindowIcon(QIcon(QPixmap("icon.png")));
    mScoreBtns << ui->ZeroPtBtn << ui->OnePtBtn
               << ui->TwoPtBtn << ui->ThreePtBtn
               << ui->FourPtBtn;

    mBagScoreBtns << ui->TwoWithBagBtn << ui->ThreeWithBagBtn
                  << ui->FourWithBagBtn << ui->FiveWithBagBtn
                  << ui->SixWithBagBtn;
    foreach(QPushButton *p, mScoreBtns)
    {
      connect(p, SIGNAL(clicked()), this, SLOT(onScoreSelected()));
    }

    foreach(QPushButton *p, mBagScoreBtns)
    {
      connect(p, SIGNAL(clicked()), this, SLOT(onBagScoreSelected()));
    }
  }

TurnScoreSelectDlg::~TurnScoreSelectDlg()
{
    delete ui;
}

void TurnScoreSelectDlg::closeEvent(QCloseEvent *e)
{
  reject();
}

int TurnScoreSelectDlg::getRoundPosBtn(QPushButton *btn)
{
  int r(1);
  if (btn != NULL)
  {
    foreach(QPushButton *b, mRoundBtns)
    {
      if (b->objectName() == btn->objectName())
      {
        return r;
      }
      else
      {
        ++r;
      }
    }
  }
  return 0;
}

void TurnScoreSelectDlg::addNewRoundBtn(QPushButton *btn, bool bag)
{
  if (btn != NULL)
  {
    if (mSelectedRound == mRoundBtns.last())
    {
      mSelectedRound->setText(btn->text()+(bag?"'":""));
      if (mGame.addTurn(btn->text().toInt(), bag))
      {
        accept();
      }

      QPushButton *rbtn(new QPushButton("?", this));
      rbtn->setObjectName(
          QString("%0_%1Round%2").arg(mGame.player1())
                                 .arg(mGame.player2())
                                 .arg(mRoundBtns.count()+1)
      );
      rbtn->setMinimumWidth(1);
      rbtn->setCheckable(true);
      rbtn->setChecked(true);
      connect(rbtn, SIGNAL(clicked(bool)), this, SLOT(onRoundSelected(bool)));
      ui->roundLayout->addWidget(rbtn);
      mRoundBtns << rbtn;
      mSelectedRound->setChecked(false);
      mSelectedRound = rbtn;

      ui->turnLabel->setText((mRoundBtns.count()%2==0?mPlayer2:mPlayer1)+"'s Turn");
    }
    else
    {
      int r(getRoundPosBtn(mSelectedRound));
      mSelectedRound->setText(btn->text()+(bag?"'":""));
      if(mGame.editTurn(r-1, btn->text().toInt(), bag))
      {
        accept();
      }
    }
  }
}

void TurnScoreSelectDlg::onScoreSelected()
{
  QPushButton *btn(dynamic_cast<QPushButton*>(sender()));
  addNewRoundBtn(btn, false);
}

void TurnScoreSelectDlg::onBagScoreSelected()
{
  QPushButton *btn(dynamic_cast<QPushButton*>(sender()));
  addNewRoundBtn(btn, true);
}

void TurnScoreSelectDlg::onRoundSelected(bool checked)
{
  QPushButton *btn(dynamic_cast<QPushButton*>(sender()));
  if (btn != NULL)
  {
    if (mSelectedRound == btn && !checked)
    {
      mSelectedRound->setChecked(true);
    }
    else if (mSelectedRound != btn)
    {
      mSelectedRound->setChecked(false);
      mSelectedRound = btn;
    }
  }
}

Game TurnScoreSelectDlg::getRound(QWidget *parent, const QString &p1, const QString &p2)
{
  TurnScoreSelectDlg tssd(parent);
  tssd.mGame.setPlayers(p1, p2);
  tssd.setWindowTitle(p1 + " vs " + p2);
  tssd.raise();
  tssd.mPlayer1 = p1;
  tssd.mPlayer2 = p2;
  tssd.ui->turnLabel->setText(tssd.mPlayer1 + "'s Score?");
  QPushButton *btn(new QPushButton("?", &tssd));
  btn->setObjectName(p1+"_"+p2+"Round1");
  btn->setCheckable(true);
  btn->setChecked(true);
  btn->setMinimumWidth(1);
  tssd.connect(btn, SIGNAL(clicked(bool)), &tssd, SLOT(onRoundSelected(bool)));
  tssd.ui->roundLayout->addWidget(btn);
  tssd.mRoundBtns << btn;
  tssd.mSelectedRound = tssd.mRoundBtns.last();
  if (tssd.exec() == QDialog::Accepted)
  {
    return tssd.mGame;
  }
  else
  {
    return Game();
  }
}
